package cn.akwangl.socket.bio;

import lombok.extern.slf4j.Slf4j;

import java.util.Scanner;

@Slf4j
public class UserInputHandler implements Runnable {

    private ChatClient chatClient;

    public UserInputHandler(ChatClient chatClient) {
        this.chatClient = chatClient;
    }

    @Override
    public void run() {
        Scanner sc = new Scanner(System.in);
        log.info("如需停止程序请输入:quit 关键字并回车");

        while (chatClient.flag) {
            String userMsg = sc.nextLine();
            // 客户端发送消息给服务端
            chatClient.sendMessage(userMsg);
            // 检查是否要退出
            if (chatClient.hasCommandQuit(userMsg)) {
                chatClient.flag = false;
                break;
            }
        }
    }
}
